<?php 
namespace Admin\Controller;

use Think\Page;
use Common\Base\ControllerBase;
use Common\Logic\Admin\GroupLogic;
/**
* 分组
*/
class GroupController extends ControllerBase
{
	public $L_group;
	public function _initialize()
	{
		$this->L_group = new GroupLogic();
	}
	
	//分组列表
    public function group_list()
    {
        $list = $this->L_group->getGroupList(2);
        $rs = array();
        if( ! empty($list)){
            $list = list_to_tree($list, 'group_id', 'group_pid', '_sub');
            floatArray($list, '_sub', $rs);
        }
        unset($list);
        $this->assign('list', $rs);
        $this->display();
    }

    //分组添加
    public function group_add()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->L_group->addGroup($post);
            if(false === $rs){
                $this->error($this->L_group->getError());
            }else{
                $this->success('操作成功！', U('group/group_list'));
            }
            exit;
        }
        $this->assign('nodesCheckbox', $this->L_group->getNodesToCheckbox());
        $this->assign('groupSelect', $this->L_group->getGroupSelect());
        $this->display();
    }

    //分组修改
    public function group_edit()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->L_group->editGroup($post);
            if(false === $rs){
                $this->error($this->L_group->getError());
            }else{
                $this->success('操作成功！', U('group/group_list'));
            }
            exit;
        }

        $group_id = (int)I('get.group_id', 0);
        if($group_id < 0){ $this->error('参数错误'); }
        $detail = $this->L_group->getGroupList(4, array('group_id'=>$group_id));
        if(empty($detail)){ $this->error('参数错误'); }

        $this->assign('nodesCheckbox', $this->L_group->getNodesToCheckbox($group_id, 0, true));
        $this->assign('groupSelect', $this->L_group->getGroupSelect($detail['group_pid'], 'group_pid'));
        $this->assign('detail', $detail);
        $this->display();
    }

    //删除分组
    public function group_del()
    {
        $group_id = (int)I('get.group_id', 0); if($group_id < 1){ $this->error('参数错误'); }
        $rs = $this->L_group->delGroup($group_id);
        if(false === $rs){
            $this->error($this->L_group->getError());
        }else{
            $this->success('删除成功', U('group/group_list'));
        }
    }

    //ajax更改group状态
    public function ajax_group_st()
    {
        $st = (int)I('request.st', 0); if($st!=0 && $st!=1){ $st = 0; }
        $rs = $this->L_group->updateGroupByGroupId(array('st'=>$st), I('request.group_id', 0));
        if(false === $rs){
            $this->ajaxReturn(genReturn('n', $this->L_group->getError()));
        }else{
            $this->ajaxReturn(genReturn('y', 'ok'));
        }
    }
	
}